<template>
  <el-card class="box-card" style="margin: 10px 0px">
    <div class="header">
      <!--  @tab-click="handleClick" -->
      <!-- 头部左侧内容 -->
      <el-tabs class="tab" v-model="activeName">
        <el-tab-pane label="销售额" name="sale"></el-tab-pane>
        <el-tab-pane label="访问量" name="visite"></el-tab-pane>
      </el-tabs>
      <!-- 头部右侧内容 -->
      <div class="right">
        <!-- <el-radio-group v-model="radio1">
          <el-radio-button @change="setDay" label="今日"></el-radio-button>
          <el-radio-button @change="setWeek" label="本周"></el-radio-button>
          <el-radio-button @change="setMonth" label="本月"></el-radio-button>
          <el-radio-button @change="setYear" label="今年"></el-radio-button>
        </el-radio-group> -->
        <span
          class="tabspan"
          :class="{ cur: currentIndex == index }"
          v-for="(tab, index) in tabs"
          :key="index"
          @click="tabClick(index)"
          >{{ tab }}</span
        >
        <!-- <span @click="setDay">今日</span>
        <span @click="setWeek">本周</span>
        <span @click="setMonth">本月</span>
        <span @click="setYear">本年</span> -->

        <!--    v-model="value1" -->
        <el-date-picker
          v-model="date"
          class="date"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </div>
    </div>
    <div>
      <el-row :gutter="10">
        <el-col :span="18">
          <!-- 容器 -->
          <div class="charts" ref="charts"></div>
        </el-col>
        <el-col :span="6" class="right">
          <h3>门店{{ title }}排名</h3>
          <ul>
            <li v-for="(item, index) in list" :key="index">
              <span class="rindex">{{ index + 1 }}</span>
              <span>{{ item.name }}</span>
              <span class="rvalue">{{ item.money }}</span>
            </li>
          </ul>
        </el-col>
      </el-row>
    </div>
  </el-card>
</template>

<script>
//引入echarts
import * as echarts from 'echarts'
import dayjs from 'dayjs'
import { mapState } from 'vuex'
export default {
  name: '',
  data() {
    return {
      currentIndex: 0,

      activeName: 'sale',
      mycharts: null,
      //收集日历数据
      date: [],
      tabs: ['今日', '本周', '今月', '今年']
    }
  },
  mounted() {
    //初始化echarts实例
    this.mycharts = echarts.init(this.$refs.charts)

    window.addEventListener('resize', () => {
      this.mycharts.resize()
    })
    // this.mycharts.setOption({
    //   title: {
    //     text: this.title + '趋势'
    //   },
    //   tooltip: {
    //     trigger: 'axis',
    //     axisPointer: {
    //       type: 'shadow'
    //     }
    //   },
    //   grid: {
    //     left: '3%',
    //     right: '4%',
    //     bottom: '3%',
    //     containLabel: true
    //   },
    //   xAxis: [
    //     {
    //       type: 'category',
    //       data: [],
    //       axisTick: {
    //         alignWithLabel: true
    //       }
    //     }
    //   ],
    //   yAxis: [
    //     {
    //       type: 'value'
    //     }
    //   ],
    //   series: [
    //     {
    //       name: 'Direct',
    //       type: 'bar',
    //       barWidth: '40%',
    //       data: []
    //     }
    //   ]
    // })

    //顶部是mounted：为什么第一次没有数据，没有数据因此不显示
  },
  computed: {
    //计算属性-标题
    title() {
      //重新设置配置项
      return this.activeName == 'sale' ? '销售额' : '访问量'
    },
    list() {
      return this.activeName == 'sale'
        ? this.listState.orderRank
        : this.listState.userRank
    },
    ...mapState({
      listState: (state) => state.Home.list
    })
  },
  watch: {
    listState() {
      this.mycharts.setOption({
        title: {
          text: this.title + '趋势'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: this.listState.orderFullYearAxis,
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: 'Direct',
            type: 'bar',
            barWidth: '60%',
            data: this.listState.orderFullYear,
            color: 'yellowgreen'
          }
        ]
      })
    },
    title() {
      //图标配置数据可以再次修改，如果有新的数值，新的数值，没有新的数值，还是用以前的
      this.mycharts.setOption({
        title: {
          text: this.title
        },
        xAxis: {
          data:
            this.title == '销售额'
              ? this.listState.orderFullYearAxis
              : this.listState.userFullYearAxis
        },
        series: [
          {
            name: 'Direct',
            type: 'bar',
            barWidth: '60%',
            data:
              this.title == '销售额'
                ? this.listState.orderFullYear
                : this.listState.userFullYear,
            color: this.title == '访问量' ? 'yellowgreen' : ''
          }
        ]
      })
    }
  },
  //监听属性

  methods: {
    tabClick(index) {
      this.currentIndex = index
      switch (index) {
        case 0:
          this.setDay()
          break
        case 1:
          this.setWeek()
          break
        case 2:
          this.setMonth()
          break
        case 3:
          this.setYear()
          break
        default:
          break
      }
    },
    //本天
    setDay() {
      console.log(day)
      const day = dayjs().format('YYYY-MM-DD')
      this.date = [day, day]
    },
    //本周
    setWeek() {
      const start = dayjs().day(1).format('YYYY-MM-DD')
      const end = dayjs().day(7).format('YYYY-MM-DD')
      this.date = [start, end]
    },
    //本月
    setMonth() {
      const start = dayjs().startOf('month').format('YYYY-MM-DD')
      const end = dayjs().endOf('month').format('YYYY-MM-DD')
      this.date = [start, end]
    },
    //本年
    setYear() {
      const start = dayjs().startOf('year').format('YYYY-MM-DD')
      const end = dayjs().endOf('year').format('YYYY-MM-DD')
      this.date = [start, end]
    }
  }
}
</script>

<style scoped>
.header {
  position: relative;
  display: flex;
  justify-content: space-between;
}
.tab {
  width: 100%;
}
.cur {
  background: #3d9fff;
  color: #fff;
}
.right {
  position: absolute;
  right: 0px;
  top: -10px;
}
.date {
  width: 250px;
  margin: 0px 20px;
}
.right .tabspan {
  padding: 9px 15px;
  border: 1px solid #eee;
  font-size: 12px;
  cursor: pointer;
}
.charts {
  width: 100%;
  height: 300px;
}
ul {
  list-style: none;
  width: 100%;
  height: 300px;
  padding: 0px;
}
ul li {
  height: 8%;
  margin: 10px;
}
.rindex {
  float: left;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: black;
  color: white;
  text-align: center;
  margin: 0 10px;
}
.rvalue {
  float: right;
}
</style>
